﻿// Decompiled with JetBrains decompiler
// Type: DnProcessSimulateCommands.StopZero.CApStopZeroMotionVolumeCreator
// Assembly: DnProcessSimulateCommands, Version=2402.0.0.890, Culture=neutral, PublicKeyToken=null
// MVID: A885D881-AEDF-447E-852A-6F13102E089C
// Assembly location: C:\Program Files\Tecnomatix_2402\eMPower\DotNetCommands\DnProcessSimulateCommands.dll

using EngineeringInternalExtension;
using EngineeringInternalExtension.DataTypes;
using System;
using System.Collections.Generic;
using Tecnomatix.Engineering;


namespace DnProcessSimulateCommands.StopZero;

public sealed class CApStopZeroMotionVolumeCreator
{
  private ITxRoboticLocationOperation _startLocation;
  private ITxRoboticLocationOperation _endLocation;
  private ITxRobot _robot;
  private ICollection<ITxObject> _spheres;
  private bool _accuracyMode;
  private double _timeInterval;
  private int _stopType;
  private TxColor _riskZoneColor;
  private ITxGeometryCreation _resultScope;
  private Action<CApStopZeroProgressReporterData> _progressSetter;
  private Func<bool> _isGenerationStopped;

  public CApStopZeroMotionVolumeCreator(
    ITxRoboticLocationOperation start,
    ITxRoboticLocationOperation end,
    ICollection<ITxObject> spheres,
    bool accuracyMode,
    double timeInterval,
    int stopType,
    TxColor color,
    ITxGeometryCreation scope,
    Action<CApStopZeroProgressReporterData> progressSetter,
    Func<bool> isGenerationStopped)
  {
    if (CApStopZeroUtilities.AreInSameOrder(start, end))
    {
      this._startLocation = start;
      this._endLocation = end;
    }
    else
    {
      this._startLocation = end;
      this._endLocation = start;
    }
    this._spheres = (ICollection<ITxObject>) new List<ITxObject>((IEnumerable<ITxObject>) spheres);
    this._accuracyMode = accuracyMode;
    this._timeInterval = timeInterval;
    this._stopType = stopType;
    this._resultScope = scope;
    this._riskZoneColor = color;
    this._robot = CApStopZeroUtilities.GetAssignedRobot(this._startLocation ?? this._endLocation);
    this._progressSetter = progressSetter;
    this._isGenerationStopped = isGenerationStopped;
  }

  public bool Create()
  {
    CApStopZeroProgressReporter progressReporter = new CApStopZeroProgressReporter(this._startLocation, this._endLocation, this._progressSetter);
    progressReporter.ReportStart();
    List<CApStopZeroSafetyVolume> sefetyVolumes = this.GetSefetyVolumes(this._spheres);
    List<List<TxRobotSimulationStopMotionData>> stopMotionData;
    bool flag = new CApStopZeroMotionDataGenerator(this._startLocation, this._endLocation, this._accuracyMode, this._timeInterval, this._stopType, sefetyVolumes, progressReporter, this._isGenerationStopped).Generate(out stopMotionData);
    if (flag)
    {
      CApStopZeroMotionVolumeGraphicsCreator volumeGraphicsCreator = new CApStopZeroMotionVolumeGraphicsCreator(sefetyVolumes, stopMotionData, this._timeInterval, this._riskZoneColor, this._resultScope);
      progressReporter.ReportMotionVolumeProgress(true);
      volumeGraphicsCreator.Create();
      progressReporter.ReportMotionVolumeProgress(false);
    }
    progressReporter.ReportEnd();
    return flag;
  }

  private CApStopZeroSphereInfo GetSpereInfo(ITxObject obj)
  {
    try
    {
      TxVector txVector;
      double radius;
      TxGraphicsUtilitiesEx.GetSphereParameters(obj, ref txVector, ref radius);
      return new CApStopZeroSphereInfo(TxTransformation.op_Multiply(this._robot.Toolframe.AbsoluteLocation.Inverse, new TxTransformation(txVector, (TxTransformation.TxTransformationType) 1)), radius);
    }
    catch (TxInvalidObjectException ex)
    {
      return (CApStopZeroSphereInfo) null;
    }
  }

  private List<CApStopZeroSafetyVolume> GetSefetyVolumes(ICollection<ITxObject> spheres)
  {
    List<CApStopZeroSafetyVolume> sefetyVolumes = new List<CApStopZeroSafetyVolume>();
    int id = 0;
    foreach (ITxObject sphere in (IEnumerable<ITxObject>) spheres)
    {
      CApStopZeroSphereInfo spereInfo = this.GetSpereInfo(sphere);
      if (spereInfo != null)
      {
        sefetyVolumes.Add(new CApStopZeroSafetyVolume(id, spereInfo));
        ++id;
      }
    }
    return sefetyVolumes;
  }
}
